package com.si.sea_island.controller;


import com.si.sea_island.base.JsonResult;
import com.si.sea_island.base.ResultCode;
import com.si.sea_island.mapper.UserDBMapper;
import com.si.sea_island.mapper.VerifyMapper;
import com.si.sea_island.pojo.UserDB;
import com.si.sea_island.pojo.VerifyDB;
import com.si.sea_island.pojo.game.dtsData;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description fksController
 * @Author yujiangbo
 * @Date 2025-01-02
 */
@RestController
@Api(tags = "大逃杀模块")
public class dtsGameController {

    //注入mapper
    @Autowired
    private UserDBMapper userDBMapper;

    @Autowired
    private VerifyMapper verifyMapper;



    //获取用户数据
    @PostMapping("/api/member/my")
    public JsonResult getUserData(HttpServletRequest request){
        //获取token
        String token = request.getHeader("token");
        //根据token查找当前用户
        VerifyDB verifyDBByToken = verifyMapper.getVerifyDBByToken(token);
        //判断当前用户是否存在
        if (verifyDBByToken == null){
            return JsonResult.error(ResultCode.user_not_find,"用户不存在");
        }
        //获得用户id
        int uid = verifyDBByToken.getUid();

        //获取用户信息
        UserDB userById = userDBMapper.getUserById(uid);
        if (userById == null){
            return JsonResult.error(ResultCode.user_not_find,"用户不存在");
        }
        //获得用户头像，昵称，id
        String avatar = userById.getAvatar();
        String nickname = userById.getNickName();
        int id = userById.getId();
        //根据用户id查询用户coinBlance
        int coinBlance = userDBMapper.getcoinBlance(id);
        //获得用户token
        String userToken = verifyDBByToken.getToken();

        //封装数据返回
        dtsData dtsData = new dtsData();
        dtsData.setAvatar(avatar);
        dtsData.setNickName(nickname);
        dtsData.setId(id);
        dtsData.setUserToken(userToken);
        dtsData.setCoinBlance(coinBlance);
        //返回数据给前端
        return JsonResult.success("获取成功").data(dtsData);
    }


}
